﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BudgetSV.Common;
using BudgetData;

namespace BudgetSV.admin.manager
{
    public partial class depart_de_edit : ManagePage
    {
        private string action = DTEnums.ActionEnum.Add.ToString(); //操作类型
        private long id = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            string _action = DTRequest.GetQueryString("action");
            this.id = DTRequest.GetQueryInt("id");

            if (!string.IsNullOrEmpty(_action) && _action == DTEnums.ActionEnum.Edit.ToString())
            {
                this.action = DTEnums.ActionEnum.Edit.ToString();//修改类型
                if (this.id == 0)
                {
                    JscriptMsg("传输参数不正确！", "back", "Error");
                    return;
                }
            }
            if (!Page.IsPostBack)
            {
                ChkAdminLevel("depart_de_list", DTEnums.ActionEnum.View.ToString()); //检查权限
                DepartTreeBind(); //绑定部门列表
                if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
                {
                    ShowInfo(this.id);
                }
                else
                {
                    if (this.id > 0)
                    {
                        this.ddlParentId.SelectedValue = this.id.ToString();
                    }
                    txtTitle.Attributes.Add("ajaxurl", "../../tools/admin_ajax.ashx?action=depart_validate");
                }
            }
        }

        #region 绑定部门列表=============================
        private void DepartTreeBind()
        {
            List<Department> dt = GetDepartList(GetAdminInfo().org_id);

            this.ddlParentId.Items.Clear();
            this.ddlParentId.Items.Add(new ListItem("无父级导航", "0"));
            foreach (var dr in dt)
            {
                string Id = dr.id.ToString();
                int ClassLayer = int.Parse(dr.class_layer.ToString());
                string Title = dr.name.ToString().Trim();

                if (ClassLayer == 1)
                {
                    this.ddlParentId.Items.Add(new ListItem(Title, Id));
                }
                else
                {
                    Title = "├ " + Title;
                    Title = Utils.StringOfChar(ClassLayer - 1, "　") + Title;
                    this.ddlParentId.Items.Add(new ListItem(Title, Id));
                }
            }
        }
        #endregion

        #region 赋值操作=================================
        private void ShowInfo(long _id)
        {
            Department model = Mod.GetModel<Department>(c=>c.id==id);

            ddlParentId.SelectedValue = model.parent_id.ToString();
            txtSortId.Text = model.sort_id.ToString();
            if (model.is_lock == 1)
            {
                cbIsLock.Checked = true;
            }
            cbGreat.Checked = model.Great;
            txtTitle.Text = model.name;
            txtTitle.Attributes.Add("ajaxurl", "../../tools/admin_ajax.ashx?action=depart_validate&old_name=" + Utils.UrlEncode(model.name));
            txtTitle.Focus(); //设置焦点，防止JS无法提交
            if (model.is_sys == 1)
            {
                ddlParentId.Enabled = false;
                txtTitle.ReadOnly = true;
            }
            txtTitle.Text = model.name;
            txtRemark.Text = model.remark;
        }
        #endregion

        #region 增加操作=================================
        private bool DoAdd()
        {
            //try
            //{
                Department model = new Department();

                model.name = txtTitle.Text.Trim();
                model.sort_id = int.Parse(txtSortId.Text.Trim());
                model.is_lock = 0;
                if (cbIsLock.Checked == true)
                {
                    model.is_lock = 1;
                }
                model.Great = cbGreat.Checked;
                model.remark = txtRemark.Text.Trim();
                model.parent_id = int.Parse(ddlParentId.SelectedValue);
                Manager manager = GetAdminInfo();
                model.org_id = manager.org_id;

            model = Mod.Add(model);
                if (model.id>0)
                {
                    this.id = model.id;
                    AddAdminLog(DTEnums.ActionEnum.Add.ToString(), "添加处室信息:" + model.name); //记录日志

                    if (model.Great)
                    {
                        //增加机构
                        Add(model);
                        AddAdminLog(DTEnums.ActionEnum.Add.ToString(), "添加大处室的机构信息：" + model.name);
                    }
                    return true;
                }
            //}
            //catch
            //{
            //    return false;
            //}
            return false;
        }

        /// <summary>
        /// 大处室同时增加机构
        /// </summary>
        /// <param name="dept"></param>
        private void Add(Department dept)
        {
            var sup = Mod.GetModel<Organization>(c=>c.id==dept.org_id);
            Organization org = Mod.GetModel<Organization>(t => t.dept_id == dept.id);
            if (org != null)
            {
                if (dept.Great)
                {
                    org.class_layer = sup.class_layer + 1;
                    org.is_department = true;
                    org.is_lock = 1;
                    org.is_sys = 0;
                    org.name = dept.name;
                    org.parent_id = sup.id;
                }
                else
                {
                    org.is_sys = 1; //无效
                }
                Mod.Update(org);
            }
            else
            {
                org = new Organization
                {
                    class_layer = sup.class_layer + 1,
                    dept_id = dept.id,
                    is_department = true,
                    is_lock = 1,
                    is_sys = 0,
                    name = dept.name,
                    parent_id = sup.id
                };

                Mod.Add(org);
            }
        }

        #endregion

        #region 修改操作=================================

        private bool DoEdit(long _id)
        {
            //try
            //{
            Department model = Mod.GetModel<Department>(c => c.id == id);
            var isGreat = model.Great;

                model.name = txtTitle.Text.Trim();
                model.sort_id = int.Parse(txtSortId.Text.Trim());
                model.is_lock = 0;
                if (cbIsLock.Checked == true)
                {
                    model.is_lock = 1;
                }
                model.Great = cbGreat.Checked;
                model.remark = txtRemark.Text.Trim();
                if (model.is_sys == 0)
                {
                    int parentId = int.Parse(ddlParentId.SelectedValue);
                    //如果选择的父ID不是自己,则更改
                    if (parentId != model.id)
                    {
                        model.parent_id = parentId;
                    }
                }

                if (Mod.Update(model)>0)
                {
                    AddAdminLog(DTEnums.ActionEnum.Add.ToString(), "修改部门信息:" + model.name); //记录日志

                    //增加机构
                    isGreat = isGreat || model.Great;
                    if (isGreat)
                    {
                        Add(model);
                        AddAdminLog(DTEnums.ActionEnum.Add.ToString(), "添加大处室的机构信息：" + model.name);
                    }
                    return true;
                }
            //}
            //catch
            //{
            //    return false;
            //}
            return false;
        }



        #endregion

        //保存
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
            {
                ChkAdminLevel("depart_de_list", DTEnums.ActionEnum.Edit.ToString()); //检查权限
                if (!DoEdit(this.id))
                {
                    JscriptMsg("保存过程中发生错误！", "", "Error");
                    return;
                }
                JscriptMsg("修改部门成功！", "depart_de_list.aspx", "Success", "parent.loadMenuTree(true)");
            }
            else //添加
            {
                ChkAdminLevel("depart_de_list", DTEnums.ActionEnum.Add.ToString()); //检查权限
                if (!DoAdd())
                {
                    JscriptMsg("保存过程中发生错误！", "", "Error");
                    return;
                }
                JscriptMsg("添加部门成功！", "depart_de_list.aspx", "Success", "parent.loadMenuTree(true)");
            }
        }

    }
}